﻿using System.Collections;
using System.Collections.ObjectModel;
using System.Data;
using System.Globalization;
using SQLDiagConfiguration.Properties;

namespace SQLDiagConfiguration
{
    /// <summary>
    ///  This class holds all state for the application including the DataSet which represents
    ///  the configuration file.
    /// </summary>
    public class  DiagInfoObject 
    {
        #region Properties

        private string configPath = Resources.strConfigPath;
        public string ConfigPath
        {
            get { return configPath; }
            set { configPath = value; }
        }

        private string configFile = Resources.strConfigFile;
        public string ConfigFile
        {
            get { return configFile; }
            set { configFile = value; }
        }
        public string FullConfigPath
        {
         get {return ConfigPath + Resources.strBackSlash + ConfigFile;}
        }
        private DataSet sqlDiagConfigDataSet;
        public DataSet SQLDiagConfigDataSet
        {
            get { return sqlDiagConfigDataSet; }
            set { sqlDiagConfigDataSet = value; }
        }

        private SQLDiagConfigurationOptions parent;

        public SQLDiagConfigurationOptions Parent
        {
            get { return parent; }
            set { parent = value; }
        }
        private bool isValid = false;

        public bool IsValid
        {
            get 
            {
                // first check to see if we are trying to edit a PSSDiag config file
                if (SQLDiagConfigDataSet.Tables[Resources.strPSSDiagMgr] != null)
                {
                    InvalidFileType = Resources.strPSSInvalidMessage;
                }
                if(SQLDiagConfigDataSet.Tables[Resources.strMachine] == null)
                {
                    RequiredElements.Add(Resources.strMachine); 
                }
                if( SQLDiagConfigDataSet.Tables[Resources.strInstance] == null )
                {
                    RequiredElements.Add(Resources.strInstance);
                }
                //if( SQLDiagConfigDataSet.Tables[Resources.strAnalyzer]  == null )
                //{
                //    RequiredElements.Add(Resources.strAnalyzer);
                //}
                //if( SQLDiagConfigDataSet.Tables[Resources.strProducer]  == null )
                //{
                //    RequiredElements.Add(Resources.strProducer);
                //}
                if( SQLDiagConfigDataSet.Tables[Resources.strBlockingCollector] == null )
                {
                    RequiredElements.Add(Resources.strBlockingCollector);
                }
                if( SQLDiagConfigDataSet.Tables[Resources.strSQLDiagCollector] == null )
                {
                    RequiredElements.Add(Resources.strSQLDiagCollector);
                }
                //if( SQLDiagConfigDataSet.Tables[Resources.strEventlogType] == null )
                //{
                //    RequiredElements.Add(Resources.strEventlogType);
                //}
                if( SQLDiagConfigDataSet.Tables[Resources.strEventlogCollector] == null )
                {
                    RequiredElements.Add(Resources.strEventlogCollector);
                }
                if( SQLDiagConfigDataSet.Tables[Resources.strPerfmonCollector] == null )
                {
                    RequiredElements.Add(Resources.strPerfmonCollector);
                }
                if( SQLDiagConfigDataSet.Tables[Resources.strProfilerCollector] == null )
                {
                    RequiredElements.Add(Resources.strProfilerCollector);
                }
                if(requiredElements.Count > 0 )
                {
                    InvalidFileType += Resources.strMissingElements;
                    foreach(string s in RequiredElements)
                    {
                        InvalidFileType += "\t"+ s + "\r\n";
                    }
                }
                if(string.IsNullOrEmpty(InvalidFileType))
                {
                    isValid = true;
                }
                return isValid; 
            }
            // This is a read only property
            //set { isValid = value; }
        }
        private string invalidFileType;

        public string InvalidFileType
        {
            get { return invalidFileType; }
            set { invalidFileType = value; }
        }
        //private List<string> requiredElements;
        private Collection<string> requiredElements;
        public Collection<string> RequiredElements
        {
            get { return requiredElements; }
            set { requiredElements = value; }
        }
        private string version;
        public string Version
        {
            get { return version; }
            set { version = value; }
        }

        private SortedList profilerEvents;
        public SortedList ProfilerEvents
        {
            get { return profilerEvents; }
            set { profilerEvents = value; }
        }

        private ArrayList perfmonObjects;
        public ArrayList PerfmonObjects
        {
            get { return perfmonObjects; }
            set { perfmonObjects = value; }
        }
        #endregion Properties
        #region Constructor
        public DiagInfoObject(SQLDiagConfigurationOptions parent)
        {
            Parent = parent;
            sqlDiagConfigDataSet = new DataSet();
            sqlDiagConfigDataSet.DataSetName = "Full";
            sqlDiagConfigDataSet.Locale = CultureInfo.CurrentCulture;
            InvalidFileType = Resources.strEmptyString;
            RequiredElements = new Collection<string>();
            ProfilerEvents = new SortedList();
            PerfmonObjects = new ArrayList();
        }
        #endregion Constructor

        #region Helper Methods
        /// <summary>
        /// This method is used to disassociate the DataSet from the XmlDataDocument.
        /// The DataSet can only be associated with one XmlDataDocument
        /// </summary>
        public void Reset()
        {
            // Remove the DataSet
            SQLDiagConfigDataSet = null;

            // Clear the collections
            PerfmonObjects.Clear();
            ProfilerEvents.Clear();

            // Create a new DataSet, ready for loading
            SQLDiagConfigDataSet = new DataSet();
            sqlDiagConfigDataSet.DataSetName = "Full";
            sqlDiagConfigDataSet.Locale = CultureInfo.CurrentCulture;
            InvalidFileType = "";
            RequiredElements.Clear();
            isValid = false;
        }
        #endregion Helper Methods
    }
}
